<!--
  @文件名：mainPage
  @来源：Winford.Wang|2022/6/23 0023，21:21
  @描述：
-->
<template>
  <div class="mainPage" >
<!--    圆点-->
    <Dots  v-if="false"/>
<!--    乌龟-->
    <tortoise/>
<!--    设置-->
    <SettingHome v-if="isSettingShow" @switchDiaSetting="switchDiaSetting"/>
  </div>
</template>

<script>
import Dots from '@/views/pets/dots'
import { ipcRenderer } from 'electron'
import { BrowserWindow } from '@electron/remote'
import PARAME from '@/complete/param/parameterList'
import SettingHome from '@/views/setting/settingHome'
import Tortoise from '@/views/pets/tortoise'

const win = BrowserWindow.getFocusedWindow()

export default {
  name: 'mainPage',
  data () {
    return {
      timerInstance: null,
      isSettingShow: false
    }
  },
  created () {

  },
  mounted () {
    // 监听鼠标移动
    window.addEventListener('mousemove', event => {
      this.handleMouseMov(event)
    });
    // 打开设置
    ipcRenderer.on(PARAME.SET_SETTINT_PAGE_APP, (e, data) => {
      this.switchDiaSetting(data)
    })
  },
  activated () {

  },
  methods: {
    // 是否开启关闭设置
    switchDiaSetting(data) {
      this.isSettingShow = data
    },
    // 返回画布坐标
    getScreenPoint: () => ipcRenderer.invoke(PARAME.GET_SCREEN_POINT_APP),
    // 设置是否可点击
    handleMouseMov(e) {
      const _this = this
      if (e.target === document.documentElement) {
        win.setIgnoreMouseEvents(true);
        if (_this.timerInstance) clearInterval(_this.timerInstance);
        _this.timerInstance = setInterval(function () {
          _this.getScreenPoint().then(point => {
            const elem = document.elementFromPoint(
              Math.floor(point.x),
              Math.floor(point.y)
            );
            if (elem !== document.documentElement) {
              win.setIgnoreMouseEvents(false);
              clearInterval(_this.timerInstance);
            }
          })
        }, 150);
      }
      else {
        win.setIgnoreMouseEvents(false);
      }
    }
  },
  deactivated () {

  },
  unmounted () {
    window.removeEventListener('mousemove', event => {
      this.handleMouseMov(event)
    })
  },
  components: { Tortoise, SettingHome, Dots }
}
</script>

<style scoped lang='scss'>
.mainPage {
  height: 100%;
  width: 100%;
  position: relative;
  z-index: 0;
  margin: 0;
  padding: 0;
}
</style>
